﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;

namespace PassBag.Utils {

    /// <summary>
    /// Help to determine wich on wich operating system the application is running.
    /// </summary>
    public sealed class OSInfo {

        /// <summary>
        /// List of supported os
        /// </summary>
        public enum OSInformation {
            UnsuportedOS = 1,
            UnixLinux,
            MacOSX,
            Windows,
        }

        /// <summary>
        /// Gets the current OS.
        /// </summary>
        /// <returns>Return a OSInformation  that represented the current OS.</returns>
        public static OSInformation GetCurrentOS () {
            string os = Environment.OSVersion.ToString ().ToLower ();
            int p = (int)Environment.OSVersion.Platform;
            int osxIndice = 0;

            if (os.Contains ("microsoft") || os.Contains ("windows")) {
                return OSInformation.Windows;
            }
            else if ((p == 4) || (p == 6) || (p == 128)) {

                if (System.IO.Directory.Exists ("/Applications/Safari.app"))
                    osxIndice++;
                if (System.IO.Directory.Exists ("/Applications/Finder.app"))
                    osxIndice++;
                if (System.IO.Directory.Exists ("/Library/Automator/"))
                    osxIndice++;
                if (System.IO.Directory.Exists (Environment.GetEnvironmentVariable ("HOME") + "/Library/Mail/"))
                    osxIndice++;
                if (System.IO.Directory.Exists (Environment.GetEnvironmentVariable ("HOME") + "/Library/iTunes/"))
                    osxIndice++;
                if (System.IO.Directory.Exists (Environment.GetEnvironmentVariable ("HOME") + "/Library/Keychains/"))
                    osxIndice++;

                if (osxIndice > 3) {
                    return OSInformation.MacOSX;
                }
                else {
                    return OSInformation.UnixLinux;
                }
            }
            else {
                return OSInformation.UnsuportedOS;
            }
        }

    }
}
